package com.baihan.angus.app.game.busshow.ui.script_library.genuine_store;

import android.os.Bundle;

import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.baihan.angus.app.game.basic.base.BaseActivity;
import com.baihan.angus.app.game.busshow.BR;
import com.baihan.angus.app.game.busshow.R;
import com.baihan.angus.app.game.busshow.adps.StoreListAdp;
import com.baihan.angus.app.game.busshow.databinding.ActivityStoreListBinding;
import com.baihan.angus.app.game.common.arouter_path.ArouterAppPath;
import com.baihan.angus.app.game.common.bean.response.StoreResponse;

import java.util.HashMap;
import java.util.Map;

/**
 * 店家列表
 */
@Route(path = ArouterAppPath.STORE_LIST_ACTIVITY)
public class StoreListActivity extends BaseActivity<ActivityStoreListBinding, StoreListVM> {

    private StoreListAdp mStoreListAdp;
    private Map<String, Object> mStoreRequest = new HashMap<>();

    private int mPage = 1;
    private int mSize = 10;
    private int totalPage = 0;

    @Override
    protected int getContentLayout() {
        return R.layout.activity_store_list;
    }

    @Override
    protected int getViewModelId() {
        return BR.storeListVM;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mVDBinding.ivBack.setOnClickListener(view -> finish());

        mStoreListAdp = new StoreListAdp();
        mVDBinding.rvStoreList.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
        mStoreListAdp.bindToRecycleView(mVDBinding.rvStoreList);

        mVDBinding.srlStoreList.setOnRefreshListener(refreshLayout -> {
            mPage = 1;
            mStoreRequest.put("page", mPage);
//            mViewModel.getAppointAreaRoom(mParameter);
        });

        mVDBinding.srlStoreList.setOnLoadMoreListener(refreshLayout -> {
            mStoreRequest.put("page", ++mPage);
//            mViewModel.getAppointAreaRoom(mParameter);
        });

        mStoreListAdp.setOnItemClickListener(new StoreListAdp.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {

            }
        });
    }

    @Override
    protected void initData() {
        mStoreRequest.put("page", mPage);
        mStoreRequest.put("size", mSize);
        mViewModel.getStoreList(4, mStoreRequest);
    }

    @Override
    protected void initObservers() {
        super.initObservers();
        mViewModel.mldStore.observe(this, new Observer<StoreResponse.DataX>() {
            @Override
            public void onChanged(StoreResponse.DataX dataX) {
                if (mVDBinding.srlStoreList.isRefreshing()) mVDBinding.srlStoreList.finishRefresh();
                if (mVDBinding.srlStoreList.isLoading()) mVDBinding.srlStoreList.finishLoadMore();

                if (dataX == null) return;

                if (mPage == 1) {
                    mStoreListAdp.setNewData(dataX.getData());
                } else {
                    mStoreListAdp.addAll(dataX.getData());
                }

                if (dataX.getTotal() % mSize > 0) {
                    totalPage = dataX.getTotal() / mSize + 1;
                } else {
                    totalPage = dataX.getTotal() / mSize;
                }

                if (mPage < totalPage) {
                    mVDBinding.srlStoreList.setEnableLoadMore(true);
                } else {
                    mVDBinding.srlStoreList.setEnableLoadMore(false);
                }
            }
        });
    }
}